//
//  CategorySearchBar.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/30.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "CategorySearchBar.h"
@interface CategorySearchBar()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField * textField;

@property(nonatomic,strong)UIView * contentView;

@property(nonatomic,strong)UIButton * searchButton;
@end
@implementation CategorySearchBar

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        self.backgroundColor = [UIColor clearColor];
        self.contentView = [[UIView alloc] initWithFrame:self.bounds];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.contentView];
        
        self.textField = [[UITextField alloc] initWithFrame:self.contentView.bounds];
        self.textField.borderStyle = UITextBorderStyleNone;
        self.textField.clearsOnBeginEditing = YES;
        self.textField.delegate = self;
        
        self.textField.attributedPlaceholder =  [[NSAttributedString alloc] initWithString:@"请输入关键字" attributes:@{NSForegroundColorAttributeName:HexColor(@"#B2D3AF"),NSFontAttributeName:[UIFont systemFontOfSize:15]}];
        
        [self.contentView addSubview:self.textField];
        
        self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.searchButton];
        [self.searchButton addTarget:self action:@selector(searchbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
//        self.searchButton.backgroundColor = RandColor;
        [self.searchButton setBackgroundImage:[UIImage imageNamed:@"search_1_white"] forState:UIControlStateNormal];
        self.searchButton.size = CGSizeMake(Category_height(19), Category_height(19));
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self.contentView addRadius:5];
    self.contentView.layer.borderWidth = 0.5;
    self.contentView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    self.contentView.x = globalMargin;
    self.contentView.width = Category_width(266);

    
    self.searchButton.centerY = self.height*0.5;
    self.searchButton.x = self.contentView.width - self.searchButton.width - 5;
    
    self.textField.x = 10;
    self.textField.width = self.contentView.width - 10 - self.searchButton.width;
}

-(void)searchbuttonClick:(UIButton*)button{
    [self.textField resignFirstResponder];
    if(self.delegate&&[self.delegate respondsToSelector:@selector(CategorySearchBar:didClickSerchButton:)]){
        [self.delegate CategorySearchBar:self didClickSerchButton:button];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    self.text = textField.text;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
@end
